<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Validate;
class Attr extends Base
{
    public function index()
    {
        $model=model('Attr');
        $list=$model::getAttrList();
        foreach ($list as $k=>$v){
            $value_array=json_decode(json_encode($v['attr_value']),true);
            $list[$k]['value']=array_column($value_array,'value')?implode(',',array_column($value_array,'value')):' ';
            $list[$k]['extend']=array_column($value_array,'extend')?implode(',',array_column($value_array,'extend')):' ';
        }
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
    	return $this->fetch();
    }
    //加载添加/编辑页
    public function add_attr(){
    $model=model('Attr');
    $id = $this->request->param('id');
    $attrs = $model::getAttrInfo($id);
    if(!empty($attrs)){
        $value_array=json_decode(json_encode($attrs['attr_value']),true);
        $attrs['value']=array_column($value_array,'value')?implode('，',array_column($value_array,'value')):' ';
        $attrs['extend']=array_column($value_array,'extend')?implode(',',array_column($value_array,'extend')):' ';
    }

    $this->assign('data', $attrs);
    return $this->fetch();
    }

    //数据保存
    public function save_data(){
        $model=model('Attr');
        $model->startTrans();//开启事务
        try{
        $rule = [
        'name'  => 'require|max:255',
        ];

        $msg = [
            'attr.require' => '属性名称不能为空'
        ];

        $data['name'] = $this->request->param('name');
        $data['is_show'] = $this->request->param('is_show');
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)){
            throw new \Exception($validate->getError());exit();
        }

        $id = $this->request->param('id');
        $value_str = $this->request->param('value');
        $extend_str = $this->request->param('extend');

        if ($id){
            $status = $model->where('id',$id)->update($data);
            $status=$this->batchValue($value_str,$extend_str,$id);
        }else{
            $new_id = $model->insertGetId($data);
            $status=$this->batchValue($value_str,$extend_str,$new_id);
        }
        if(empty($status)){
            throw new \Exception('新增失败');exit();
        }
            $model->commit();
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            echo json_encode(array('status'=>0,'msg'=>$e->getMessage()));exit();
        }
        echo json_encode(array('status'=>1,'msg'=>'保存成功'));
    }

    /**
     * 属性值批量新增
     */
    public function batchValue($value_str,$extend_str,$id){
        $model=model('AttrValue');

        $is_update=$model->where('attr_id','=',$id)->find();
        if(!empty($is_update)){
            $model->where('attr_id','=',$id)->delete();
        }

        $value_array=explode('，',$value_str);
        $extend_array=empty($extend_str)?array():explode(',',$extend_str);
        foreach ($value_array as $key=>$val){
            $data['attr_id']=$id;
            $data['value']=$val;
            $data['extend']=empty($extend_array)?'':$extend_array[$key];
            $status = $model->insertGetId($data);
            if(empty($status)){
                return false;
            }
        }
        return true;

    }
    //删除菜单
    public function delAttr()
    {
        $model=model('Attr');
        $data['id'] = $this->request->param('id');
        $status = $model->where($data)->delete();

        //删除相关属性值
        $modelValue=model('AttrValue');
        $where['attr_id'] = $this->request->param('id');
        $status = $modelValue->where($where)->delete();
        if ($status){
            echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'删除失败']);exit();
        }
    }
}
